package org.NooLab.texx.components;

import java.util.ArrayList;


import org.NooLab.texx.TexxFactory;
import org.NooLab.texx.TexxTask;



public class TexxTasks {

	TexxFactory texxFactory;
	ArrayList<TexxTask> taskitems = new ArrayList<TexxTask>();
	private boolean stopped;
	
	// ------------------------------------------------------------------------
	public TexxTasks(TexxFactory factory) {
		//
		texxFactory = factory;
	}
	// ------------------------------------------------------------------------

	

	public int size(){
		return taskitems.size();
	}

	public TexxTask getItemByGuid(String taskGuid) {
		TexxTask sft=null, item;
		
		for (int i=0;i<taskitems.size();i++){
			item = taskitems.get(0) ;
			if (item!=null){
				if (item.getGuidID().contentEquals(taskGuid)){
					sft=item;
					break;
				}
			}
		}
		return sft;
	}

	
	public TexxTask getNextOpenTask(){
		TexxTask sft=null, item;
		
		for (int i=0;i<taskitems.size();i++){
			item = taskitems.get(0) ;
			if (item!=null){
				if (item.isCompleted()==false){
					sft=item;
					break;
				}
			}
		}
		return sft;
	}
	
	public int removeCompletedTasks(){
		int openTasks=0;
		TexxTask item;
		
		int i=taskitems.size()-1;
		while (i>=0){
		
			item = taskitems.get(0) ;
			if ((item!=null) || ( item.isCompleted() )){
				taskitems.remove(i) ;
			}
			i--;
		}
		return openTasks ;
		
	}
	
	
	public void add( TexxTask sfTask) {
		 
		TexxTask sft = getItemByGuid(sfTask.getGuidID());
		if (sft==null){
			taskitems.add(sfTask);
		}
	}

	public void clear(){
		taskitems.clear();
	}
	public TexxTask getItem( int index){
		TexxTask sft=null;
		
		if ((index>=0) && (index<taskitems.size())){
			sft = taskitems.get(index);
		}
		
		return sft;
	}
	public void remove( int index ){
		
		if ((index>=0) && (index<taskitems.size())){
			 taskitems.remove(index) ;
		}
	}

	public ArrayList<TexxTask> getTaskitems() {
		return taskitems;
	}

	public TexxFactory getSfFactory() {
		return texxFactory;
	}

	public void setSfFactory( TexxFactory factory) {
		this.texxFactory = factory;
	}

	public boolean isStopped() {
		return stopped;
	}

	public void setStopped(boolean stopped) {
		this.stopped = stopped;
	}

	public void setTaskitems(ArrayList<TexxTask> taskitems) {
		this.taskitems = taskitems;
	}
	

}
